//
//  RTJUserTool.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/5.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJUserTool.h"

#define DFL [NSUserDefaults standardUserDefaults]

static NSString *const UserDefaultsUserInfoKey = @"UserInfo";

@implementation RTJUserTool

+ (BOOL)isLogin{
    
    return [DFL objectForKey:UserDefaultsUserInfoKey] ? YES : NO;
}

+ (void)saveTheUserInfo:(NSDictionary *)info {
    
    RTJUserModel *model = [RTJUserModel yy_modelWithDictionary:info];
    model.user_id = info[@"id"];
    [DFL setObject:model.yy_modelToJSONString forKey:UserDefaultsUserInfoKey];
    [DFL synchronize];
}
+ (void)saveUserInfo:(RTJUserModel *)model{
    [DFL setObject:model.yy_modelToJSONString forKey:UserDefaultsUserInfoKey];
    [DFL synchronize];
}

+ (RTJUserModel *)getTheUserModel {
    
    NSString *modelString = [DFL objectForKey:UserDefaultsUserInfoKey];
    if (modelString) {
        return [RTJUserModel yy_modelWithJSON:modelString];
    }else{
        NSLog(@"用户未登录");
        return nil;
    }
}

+ (void)loginOut {
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //文件路径
        NSString *directoryPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        
        NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
        
        for (NSString *subPath in subpaths) {
            NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
            [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        }
        //返回主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
    });
    
    [[SDWebImageManager sharedManager].imageCache clearMemory];

    [[SDImageCache sharedImageCache] clearMemory];//清理内存
    
    [DFL removeObjectForKey:UserDefaultsUserInfoKey];
    [DFL removeObjectForKey:@"mUserDefaultsCookie"];
    [DFL removeObjectForKey:@"avatarurl"];

    [DFL synchronize];
    

}


@end
